<template>
	<view class="ai-container">
		<custom-nav title="AI填报志愿" :stylebg="styleBg" :isShowBg="false"></custom-nav>
		<view class="banner">
			<view class="left">
				<view class="title">AI志愿填报</view>
				<view class="text">精准锁定理想志愿</view>
			</view>
			<img class="img" src="/schoolCube/static/img/AI.png" alt="">
		</view>
		<view class="main">

			<view class="form">
				<view class="form-item">
					<view class="form-title">所在城市</view>
					<picker class="input" mode="selector" :range="citys" @change="changeCity" :value="formData.city - 1">
						<text>
							<text v-if="city">{{city}}</text>
							<text v-else style="color: #999">请选择所在城市</text>
						</text>
					</picker>
				</view>
				<view class="form-item">
					<view class="form-title">性别</view>
					<radio-group class="input">
						<label style="margin-right: 20rpx;" @click="formData.gender = 1">
							<radio value="1" :checked="formData.gender === 1" /><text>男</text>
						</label>
						<label @click="formData.gender = 2">
							<radio value="2" :checked="formData.gender === 2" /><text>女</text>
						</label>
					</radio-group>
				</view>
				<view>
					<view class="table">
						<table>
							<tr>
								<td colspan="6" class="title" style="height: 60rpx">主科成绩</td>
							</tr>
							<tr>
								<td>语文</td>
								<td><input type="number" v-model="formData.chinese" @input="inputValue(formData.chinese, 'chinese', 1)">
								</td>
								<td>数学</td>
								<td><input type="number" v-model="formData.math" @input="inputValue(formData.math, 'math', 1)"></td>
								<td>英语</td>
								<td><input type="number" v-model="formData.english" @input="inputValue(formData.english, 'english', 1)">
								</td>
							</tr>
							<tr>
								<td colspan="6" class="title" style="height: 60rpx">副科成绩</td>
							</tr>
							<tr>
								<td>物理</td>
								<td><input type="number" v-model="formData.otherSubject['物理']"
										@input="inputValue(formData.otherSubject['物理'], '物理')"></td>
								<td>化学</td>
								<td><input type="number" v-model="formData.otherSubject['化学']"
										@input="inputValue(formData.otherSubject['化学'], '化学')"></td>
								<td>其他1</td>
								<td><input type="number" v-model="formData.otherSubject['其他1']"
										@input="inputValue(formData.otherSubject['其他1'], '其他1')"></td>
							</tr>
							<tr>
								<td>其他2</td>
								<td><input type="number" v-model="formData.otherSubject['其他2']"
										@input="inputValue(formData.otherSubject['其他2'], '其他2')"></td>
								<td>其他3</td>
								<td><input type="number" v-model="formData.otherSubject['其他3']"
										@input="inputValue(formData.otherSubject['其他3'], '其他3')"></td>
								<td>其他4</td>
								<td><input type="number" v-model="formData.otherSubject['其他4']"
										@input="inputValue(formData.otherSubject['其他4'], '其他4')"></td>
							</tr>
							<tr>
								<td colspan="6" style="text-align: left; padding-left: 40rpx;">
									<text>总分：{{allScore}}</text>
								</td>
							</tr>
						</table>
					</view>
				</view>
			</view>
			<view class="questions">
				<view class="question-top">
					<view class="question-title">
						<text>霍兰德职业倾向测评</text>
						<text>从两个项目中选择</text>
					</view>
					<view class="question-order">
						<text>第{{curNum}}题/共{{allNum}}题</text>
					</view>
				</view>
				<view class="question-main">
					<view class="question-item">
						<view>{{curNum}}.{{curQuesion}}</view>
						<view class="answer">
							<radio-group>
								<label style="margin-right: 20rpx;">
									<radio value="1" :checked="checked === 1" @click="changeRadio(1)" /><text>是</text>
								</label>
								<label>
									<radio value="0" :checked="checked === 0" @click="changeRadio(0)" /><text>否</text>
								</label>
							</radio-group>
						</view>
					</view>
					<view class="button">
						<button type="primary" @click="open" :loading="isLoading" :disabled="isLoading">
							{{curNum === allNum ? '开始分析' : '下一题'}}
						</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getAILists,
		openAIResult,
		getAllCitys
	} from '@/requests/index.js'
	export default {
		data() {
			return {
				styleBg: "linear-gradient(180deg, #05bc81 0%, #CAFFF1 100%)",
				city: '',
				curNum: 1,
				allNum: 0,
				questions: [],
				citys: [],
				checked: -1,
				isLoading: false,
				formData: {
					math: null,
					chinese: null,
					english: null,
					otherSubject: {
						'物理': null,
						'化学': null,
						'其他1': null,
						'其他2': null,
						'其他3': null,
						'其他4': null,
					},
					score: null,
					city: '',
					gender: 0,
					hollandResult: [null, null, null, null, null, null, null, null, null, null],
				},
			}
		},
		computed: {
			allScore() {
				const math = this.formData.math;
				const chinese = this.formData.chinese;
				const english = this.formData.english;
				const other1 = this.formData.otherSubject['物理'];
				const other2 = this.formData.otherSubject['化学'];
				const other3 = this.formData.otherSubject['其他1'];
				const other4 = this.formData.otherSubject['其他2'];
				const other5 = this.formData.otherSubject['其他3'];
				const other6 = this.formData.otherSubject['其他4'];
				const allScore =  Number(math + chinese + english + other1 + other2 + other3 + other4 + other5 + other6);
				return allScore || null;
			},
			curQuesion() {
				return this.questions[this.curNum - 1] ? this.questions[this.curNum - 1].content : '';
			}
		},
		methods: {
			resetData() {
				this.city = '';
				this.curNum = 1;
				this.checked = -1;
				this.formData = {
					math: 0,
					chinese: 0,
					english: 0,
					otherSubject: {
						'物理': 0,
						'化学': 0,
						'其他1': 0,
						'其他2': 0,
						'其他3': 0,
						'其他4': 0,
					},
					score: 0,
					city: '',
					gender: 0,
					hollandResult: [null, null, null, null, null, null, null, null, null, null],
				};
			},
			async open() {
				if (!this.city) {
					uni.showToast({
						title: '请选择所在城市',
						icon: 'fail',
						duration: 1000
					});
					return;
				}
				if (!this.formData.gender) {
					uni.showToast({
						title: '请选择性别',
						icon: 'fail',
						duration: 1000
					});
					return;
				}
				if (!this.allScore) {
					uni.showToast({
						title: '请填写成绩',
						icon: 'fail',
						duration: 1000
					});
					return;
				}
				if (this.curNum === this.allNum) {
					this.formData.score = this.allScore;
					try{
						this.isLoading = true;
						const data = await openAIResult({
							...this.formData
						});
						this.isLoading = false;
						uni.setStorageSync('AI', data);
						uni.navigateTo({
							url: '/pages/ai/result'
						})
					}catch(e){
						this.isLoading = false;
					}
				} else {
					if (this.formData.hollandResult[this.curNum - 1] === null) {
						uni.showToast({
							title: '请回答当前问题',
							icon: 'fail',
							duration: 1000
						});
						return;
					}
					if (this.curNum < this.allNum) {
						this.curNum += 1;
						this.checked = -1;
					}
				}
			},
			changeCity(e) {
				this.city = this.citys[e.target.value];
				this.formData.city = Number(e.target.value + 1);
			},
			objToArr(obj) {
				let arr = []
				for (let key in obj) {
					arr.push(obj[key])
				}
				return arr;
			},
			changeRadio(value) {
				this.formData.hollandResult[this.curNum - 1] = value;
				this.checked = value;
			},
			inputValue(value, prop, type) {
				if (Number(value) < 0) {
					this.formData[prop] = 0;
				}
				const maxValue = type ? 150 : 100;
				if (Number(value) > maxValue) {
					if (type) {
						this.formData[prop] = 0;
					} else {
						this.formData.otherSubject[prop] = 0;
					}
				} else {
					if (type) {
						this.formData[prop] = Number(value);
					} else {
						this.formData.otherSubject[prop] = Number(value);
					}
				}

			}
		},
		async mounted() {
			const data = uni.setStorageSync('AI', null);
			const res1 = await getAllCitys();
			const res2 = await getAILists();
			this.citys = this.objToArr(res1);
			this.questions = res2;
			this.allNum = res2.length;
		},
		onHide() {
			this.resetData();
		}
	}
</script>

<style lang="scss" scoped>
	.ai-container {
		display: flex;
		flex-direction: column;
	}

	.main {
		flex: 1;
		overflow: auto;
	}

	.banner {
		display: flex;
		justify-content: space-between;
		padding: 0 30rpx;
		overflow: hidden;

		.title {
			font-family: PingFang SC;
			font-size: 50rpx;
			font-weight: normal;
			color: #05BC81;
			margin-bottom: 16rpx;
		}

		.text {
			font-size: 30rpx;
			line-height: 34rpx;
			color: #05BC81;
		}

		.img {
			height: 150rpx;
		}
	}

	.form {
		width: 100%;
		padding: 30rpx;
		height: 580rpx;

		.form-item {
			display: flex;
			justify-content: space-between;
			height: 80rpx;
			line-height: 80rpx;
			align-items: center;
			font-size: 28rpx;
		}

		.form-title {
			height: 80rpx;
			line-height: 80rpx;
		}

		.input,
		.table {
			margin-left: 40rpx;
			flex: 1;
			font-size: 28rpx;
		}

		.table {
			margin-left: 0rpx;
		}

		table {
			width: 100%;
			border: 1px solid #126BFF;
			border-collapse: collapse;

			tr {
				width: 100%;
			}

			td {
				border: 1px solid #126BFF;
				text-align: center;
				width: 16%;
				color: #3d3d3d;
				font-size: 28rpx;
				height: 60rpx;
			}

			input {
				background: rgba(223, 235, 255, 0.5);
				height: 100%;
				font-size: 20rpx;
			}

			.title {
				font-size: 32rpx;
				color: #333;
			}
		}
	}

	.questions {
		margin: 30rpx;
		border-radius: 30rpx;
		box-shadow: 0px 6px 9px 0px rgba(51, 51, 51, 0.1062);

		.question-top {
			height: 80rpx;
			position: relative;

			.question-title {
				background-color: #e2f3fe;
				border-top-left-radius: 20rpx;
				position: absolute;
				left: 0;
				top: 0;
				width: calc(70% + 100rpx);
				height: 100%;

				text:first-child {
					font-size: 36rpx;
					font-weight: bold;
					padding-left: 24rpx;
					line-height: 80rpx;
					color: #2979FF;
				}

				text:last-child {
					font-size: 14rpx;
					padding-left: 10rpx;
					line-height: 80rpx;
					color: #2979FF;
				}
			}

			.question-order {
				border-top-right-radius: 20rpx;
				border-bottom-left-radius: 200rpx;
				background-color: #2979FF;
				position: absolute;
				width: 30%;
				height: 100%;
				right: 0;
				top: 0;
				text-align: right;

				text {
					font-size: 24rpx;
					line-height: 80rpx;
					color: #fff;
					padding-right: 24rpx;
				}
			}
		}

		.question-main {
			width: 100%;

			.question-item {
				border-top-right-radius: 30rpx;
				font-size: 28rpx;
				line-height: 60rpx;
				padding: 16rpx;
			}

			.answer {
				text-align: right;
			}

			.button {
				padding: 30rpx;
			}
		}
	}
</style>